﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： CommentRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-07-31
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using Dapper;
using YouWei.Infrastructure.Models;
using Dapper.Extensions;

namespace YouWei.Infrastructure.Repository
{
    /// <summary>
    ///内容 仓储层
    /// </summary>	
    public class CommentRepository : BaseRepository<Comment>
    {

        #region 构造

        public CommentRepository() : base() { }

        public CommentRepository(string connectionString) : base(connectionString) { }

        public CommentRepository(DatabaseContext dbContext) : base(dbContext) { }

        #endregion

        /// <summary>
        /// 查询产品列表
        /// 此方法仅用于管理员
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Page<Comment> Pages(int pageIndex, int pageSize, CommentSearchModel model)
        {
            StringBuilder sb = new StringBuilder("SELECT * FROM [Comment] T1 WITH(NOLOCK) WHERE T1.IsActive=1");
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }

            sb.Append(" ORDER BY T1.CommentID DESC");
            return Pages(pageIndex, pageSize, sb.ToString(), dparams);
        }
        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(CommentSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {
            if (model.UserID > 0)
            {
                sb.Append(" AND T1.UserID=@UserID");
                dparams.Add("UserID", model.UserID, System.Data.DbType.Int32);
            }

            if (!string.IsNullOrWhiteSpace(model.OpenID))
            {
                sb.Append(" AND T1.OpenID=@OpenID");
                dparams.Add("OpenID", model.OpenID, System.Data.DbType.String);
            }
            else if (model.CommentID > 0)
            {
                sb.Append(" AND T1.CommentID=@CommentID");
                dparams.Add("CommentID", model.CommentID, System.Data.DbType.Int32);
            }

            if (!string.IsNullOrEmpty(model.CommentName))
            {
                sb.Append(" AND charindex(@CommentName,T1.CommentName)>0");
                dparams.Add("CommentName", model.CommentName, System.Data.DbType.String);
            }

        }
        /// <summary>
        /// 根据ID获取详细信息，包含detail
        /// </summary>
        /// <param name="CommentId"></param>
        /// <returns></returns>
        public CommentDto GetCommentInfoById(int CommentId)
        {
            //StringBuilder sb = new StringBuilder(@"select t1.*,t2.Detail,t3.NickName as WxNickName, t3.HeadImgUrl as WxHeadImgUrl 
            //from Comment t1 with(nolock) left join CommentDetail t2 on t1.CommentID=t2.CommentID ");
            //sb.Append("  Left JOIN WeChartUser t3 WITH(NOLOCK) ON t1.OpenID=t3.OpenID");
            //sb.Append(" where t1.CommentID=@CommentId");
            string sql = @" SELECT  t1.*,
                                    t2.Detail,
                                    t3.UserName AS WxNickName,
                                    t3.UserHead AS WxHeadImgUrl
                            FROM    Comment t1 WITH ( NOLOCK )
                                    LEFT JOIN CommentDetail t2  ON t1.CommentID = t2.CommentID
                                    LEFT JOIN UserInfo t3 WITH ( NOLOCK )   ON t1.UserID = t3.UserID
                            WHERE   t1.CommentID = @CommentId";
            return dbContext.Connection.QueryFirstOrDefault<CommentDto>(sql, new { CommentId = CommentId });
        }

        public Page<CommentDto> Pages(int pageIndex, int pageSize, string openID, int Section, int isHot, string orderName, string orderAsc = "ASC")
        {
            //修改关联表
            //string sql = "SELECT t1.*,t2.NickName as WxNickName, t2.HeadImgUrl as WxHeadImgUrl FROM [Comment] t1 WITH(NOLOCK) Left JOIN WeChartUser t2 WITH(NOLOCK) ON t1.OpenID=t2.OpenID ";
            //sql += "  WHERE IsActive=1 AND IsShow=1";
            string sql = @" SELECT  t1.*,
                                    t2.UserName AS WxNickName,
                                    t2.UserHead AS WxHeadImgUrl
                            FROM    [Comment] t1 WITH ( NOLOCK )
                                    LEFT JOIN UserInfo t2 WITH ( NOLOCK ) ON t1.UserID = t2.UserID
                            WHERE    t1.IsActive = 1 AND  t1.IsShow = 1";
            //热门推荐不区分板块类别
            if (isHot <= 0)
            {
                sql += " AND  t1.CommentSection=" + Section;
            }
            else
            {
                sql += " AND t1.IsHot=" + isHot;
            }

            sql += "   ORDER BY t1." + orderName + "  " + orderAsc;
            return dbContext.Connection.Page<CommentDto>(pageIndex, pageSize, sql);
        }

        public CommentDto GetTop(int section)
        {
            string sql = @" SELECT  t1.*,
                                    t2.UserName AS WxNickName,
                                    t2.UserHead AS WxHeadImgUrl
                            FROM    [Comment] t1 WITH ( NOLOCK )
                                    LEFT JOIN UserInfo t2 WITH ( NOLOCK ) ON t1.UserID = t2.UserID
                            WHERE    t1.IsActive = 1 AND  t1.IsShow = 1 ";            
                sql += "AND t1.CommentSection=" + section;         
                sql += "AND t1.IsHot=2";          
            
            return dbContext.Connection.QueryFirstOrDefault<CommentDto>(sql);
        }

        /// <summary>
        /// 添加点赞
        /// </summary>
        /// <param name="CommentId"></param>
        public void AddPraiseCount(int CommentId)
        {
            Execute("UPDATE [Comment] SET PraiseCount=PraiseCount+1 WHERE CommentID=@CommentId", new { CommentId = CommentId });
        }

        /// <summary>
        /// 添加浏览
        /// </summary>
        /// <param name="CommentId"></param>
        public void AddPageViews(int CommentId)
        {
            Execute("UPDATE [Comment] SET PageViews=PageViews+1 WHERE CommentID=@CommentId", new { CommentId = CommentId });
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="CommentId"></param>
        public void AddEvaluateCount(int CommentId)
        {
            Execute("UPDATE [Comment] SET EvaluateCount=EvaluateCount+1 WHERE CommentID=@CommentId", new { CommentId = CommentId });
        }
        /// <summary>
        /// 根据IsHot筛选
        /// </summary>
        /// <param name="IsHot"></param>
        /// <returns></returns>
        public List<Comment> GetCommentByIsHot(int IsHot)
        {
            var sql = @"SELECT * FROM [Comment] T1 WITH(NOLOCK) WHERE T1.IsActive=1 AND T1.IsHot=@IsHot";
            return Query(sql, new { IsHot });
        }
        /// <summary>
        /// 取消丽人馆置顶
        /// </summary>
        /// <param name="IsHot"></param>
        /// <returns></returns>
        public void CancelCommentIsHot()
        {
            var sql = @"update comment set ishot=0 where ishot=2";
            Execute(sql);
        }
    }
}